package com.tanhua.server.controller;

import cn.hutool.core.util.ObjectUtil;
import com.tanhua.common.vo.ErrorResult;
import com.tanhua.server.service.QuanZiService;
import com.tanhua.server.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author 王加鑫
 * @Date 2021/09/08 20:10
 * @Since JDK 1.8
 * 圈子功能中的评论
 */
@RestController
@RequestMapping("comments")
@Slf4j
public class QuanZiCommentController {

    @Autowired
    private QuanZiService quanZiService;

    /**
     * 查询评论列表
     * @return
     */
    @GetMapping
    public ResponseEntity<PageResult> queryCommentsList(@RequestParam("movementId") String publishId,
                                                        @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                        @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        try {
            PageResult pageResult = this.quanZiService.queryCommentList(publishId,page,pageSize);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            log.error("查询评论列表失败，publishId = " +publishId,e);
        }
        return ResponseEntity.status(500).build();
    }

    /**
     * 发布评论
     * @param parm
     * @return
     */
    @PostMapping
    public ResponseEntity<ErrorResult> saveComments(@RequestBody Map<String, String> parm) {
        String publishId = parm.get("movementId");
        String content = parm.get("comment");
        try {
            Boolean flag = this.quanZiService.saveComments(publishId,content);
            if(flag){
                return ResponseEntity.ok(null);
            }
            if (!flag){
                ErrorResult errorResult = ErrorResult.builder().errCode("000001").errMessage("该账号发布评论功能被冻结,无法发布评论").build();
                return ResponseEntity.status(500).body(errorResult);
            }
        } catch (Exception e) {
            log.error("保存评论失败，publishId = " + publishId,e);
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000001").errMessage("保存评论失败").build();
        return ResponseEntity.status(500).body(errorResult);
    }

    /**
     * 点赞
     * publishId 发表评论的用户的评论id
     * @param commentId
     * @return
     */
    @GetMapping("{id}/like")
    public ResponseEntity<Long> likeComment(@PathVariable("id") String commentId) {
        try {
            Long likeCount = this.quanZiService.likeComment(commentId);
            if(ObjectUtil.isNotEmpty(likeCount)){
                return ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
            log.error("点赞失败，commentId = " + commentId,e);
        }
        return ResponseEntity.status(500).build();
    }

    /**
     * 取消点赞
     * @param commentId
     * @return
     */
    @GetMapping("{id}/dislike")
    public ResponseEntity<Long> disLikeComment(@PathVariable("id") String commentId) {
        try {
            Long likeCount = this.quanZiService.disLikeComment(commentId);
            if(ObjectUtil.isNotEmpty(likeCount)){
                return ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
            log.error("取消点赞失败，commentId = " + commentId,e);
        }
        return ResponseEntity.status(500).build();
    }
}
